package pl.edu.agh.iosr.gamblingzone.dao.ejb;

import java.util.List;

import pl.edu.agh.iosr.gamblingzone.dao.UserDAO;
import pl.edu.agh.iosr.gamblingzone.model.User;

/**
 * The Class UserDAOBean.
 */
public class UserDAOBean extends GenericDAOBean<User, Long> implements UserDAO {

	/* (non-Javadoc)
	 * @see pl.edu.agh.iosr.gamblingzone.dao.UserDAO#findByLogin(java.lang.String)
	 */
	public User findByLogin(String login) {
		List<User> users = findAll();
		
		for(User u: users) {
			if(login.equals(u.getLoginName())) {
				return u;
			}
		}
			
		return null;
	}

	/* (non-Javadoc)
	 * @see pl.edu.agh.iosr.gamblingzone.dao.UserDAO#authenticateUser(java.lang.String, java.lang.String)
	 */
	public boolean authenticateUser(String login, String password) {
		
		User user = findByLogin(login);
		if(user == null) {
			return false;
		}
		if(!user.getPassword().equals(User.computeSHA1(password))) {
//		if(! ((user.getPassword()).equals(password)) ) {
			return false;
		}
		
		return true;
	}

}
